package com.vr.xuecheng.orders.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vr.xuecheng.base.exception.XueChengPlusException;
import com.vr.xuecheng.orders.entity.Orders;
import com.vr.xuecheng.orders.entity.PayRecord;
import com.vr.xuecheng.orders.mapper.PayRecordMapper;
import com.vr.xuecheng.orders.service.IPayRecordService;
import com.vr.xuecheng.orders.util.SnowFlakeUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PayRecordServiceImpl extends ServiceImpl<PayRecordMapper, PayRecord> implements IPayRecordService {
    private final PayRecordMapper payRecordMapper;
    @Override
    public PayRecord createPayRecords(Orders ordersOfAdd) {
        if (ordersOfAdd == null) {
            XueChengPlusException.cast("订单不存在");
        }
        if ("600002".equals(ordersOfAdd.getStatus())) {
            XueChengPlusException.cast("订单已支付,请不要重复下单");
        }
        // 商品交易id
        long payNo = SnowFlakeUtils.nextId();
        PayRecord payRecord = PayRecord.builder()
                .payNo(payNo)
                .orderId(ordersOfAdd.getId())
                .orderName(ordersOfAdd.getOrderName())
                .totalPrice(ordersOfAdd.getTotalPrice())
                .currency("CNY")
                .status("601001")
                .outPayChannel("alipay")
                .userId(ordersOfAdd.getUserId())
                .build();
        payRecordMapper.insert(payRecord);
        return payRecord;
    }
}
